package com.zhe.zheblogmaster.handler.admin;

import com.zhe.zheblogmaster.entity.AdminUser;
import com.zhe.zheblogmaster.service.AdminUserService;
import com.zhe.zheblogmaster.service.ConfigService;
import com.zhe.zheblogmaster.util.ResultBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author WHAOU
 * @date 2021/9/22
 * @email whaou@qq.com
 */
@Controller
@RequestMapping("/admin/configuration")
@ResponseBody
public class ConfigurationHandler {
    @Autowired
    private ConfigService configService;
    @Autowired
    private AdminUserService userService;

    @GetMapping("/info")
    public ResultBody<Map<String, String>> getConfigurationInfo() {
        Map<String, String> config = configService.getAllConfig();
        AdminUser user = userService.getUserInfo();
        if (user != null) {
            config.put("loginUserName", user.getLoginUserName());
            config.put("nickName", user.getNickName());
        }
        return ResultBody.successWithData(config);
    }

    @PostMapping("/update")
    public ResultBody<String> updateWebInfo(@RequestParam(value = "websiteName", required = false) String webName,
                                            @RequestParam(value = "websiteDescription", required = false) String description,
                                            @RequestParam(value = "websiteIcon", required = false) String icon,
                                            @RequestParam(value = "footerICP", required = false) String icp,
                                            @RequestParam(value = "yourAvatar", required = false) String avatar,
                                            @RequestParam(value = "yourName", required = false) String userName,
                                            @RequestParam(value = "yourEmail", required = false) String emial) {
        int updateResult = 0;
        if (StringUtils.hasLength(webName)) {
            updateResult += configService.updateConfig("websiteName", webName);
        }
        if (StringUtils.hasLength(description)) {
            updateResult += configService.updateConfig("websiteDescription", description);
        }
        if (StringUtils.hasLength(icon)) {
            updateResult += configService.updateConfig("websiteIcon", icon);
        }
        if (StringUtils.hasLength(icp)) {
            updateResult += configService.updateConfig("footerICP", icp);
        }

        if (StringUtils.hasLength(avatar)) {
            updateResult += configService.updateConfig("yourAvatar", avatar);
        }
        if (StringUtils.hasLength(userName)) {
            updateResult += configService.updateConfig("yourName", userName);
        }
        if (StringUtils.hasLength(emial)) {
            updateResult += configService.updateConfig("yourEmail", emial);
        }

        if (updateResult > 0) {
            return ResultBody.successWithoutData();
        } else {
            return ResultBody.error("修改失败");
        }
    }


}
